package com.digital.herbarium.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.digital.herbarium.Entity.Comment;
import com.digital.herbarium.Exception.CustomException;
import com.digital.herbarium.Mapper.CommentMapper;
import com.digital.herbarium.Util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentService {

    @Autowired
    private CommentMapper commentMapper;


    public List<Comment>  getCommentInfo (Comment comment){
        return commentMapper.getCommentInfo(comment);

    }

    public void addCommentInfo (Comment comment){
        if (StrUtil.hasEmpty(comment.getPid().toString()) || StrUtil.hasBlank(comment.getPid().toString())) {
            throw new CustomException(400, "确实科普信息id");
        }
        comment.setState(1);
        comment.setCreatetime(TimeUtil.getNowTime());
        commentMapper.addCommentInfo(comment);

    }

    public void editCommentInfo(Comment comment){
        commentMapper.editCommentInfo(comment);
    }

    public  void delCommentInfo(String id) {
        if (StrUtil.hasEmpty(id) || StrUtil.hasBlank(id)) {
            throw new CustomException(400, "缺少信息id");
        }
        String[] strArr = id.split(",");
        Long[] ids = Convert.toLongArray(strArr);
        commentMapper.delCommentInfo(ids);
    }


    public void to_top(Comment comment){
        comment.setToptime(TimeUtil.getNow());
        commentMapper.to_top(comment);
    }
}
